<?php

namespace App\Http\Controllers\Admin;

use App\Models\AccessNum;
use App\Models\ResourceTotalAccessNum;
use App\Models\ResourceUserReadBookDay;
use App\Models\UserInfo;

/**
 * 整个项目 数据分析
 * Class AnswerType
 * @package app\admin\controller
 */
class DataAnalysisController extends CommonController
{

    public $model = null;
    public function __construct()
    {
        parent::__construct();

        $this->model = new ResourceTotalAccessNum();
    }



    /**
     * 小程序访问总统计图
     * @param year   年  例如  2022   2021
     * @param month  月份  1,2,3,4,5   默认 空 按年筛选
     */
    public function accessStatistics()
    {
        $year = request()->year;
        $month = request()->month;

        if (empty($year)) {
            $year = date('Y');
        }

        list($start_time, $end_time) = get_start_end_time_by_year_month($month, $year);

        $start_time = date('Y-m-d 00:00:00', strtotime($start_time));
        $end_time = date('Y-m-d 23:59:59', strtotime($end_time));

        //访问人数
        $accessNumModel = new AccessNum();
        $get_access_statistics = $accessNumModel->getAccessStatistics($start_time, $end_time, empty($month) ? true : false);

        $data = self::disCartogramDayOrHourData($get_access_statistics, empty($month) ? $year : null, $start_time, $end_time, 'times');

        $data['access_total_number'] = $accessNumModel->getAccessNumber();
        $data['access_number'] = $accessNumModel->getAccessNumber($start_time, $end_time);

        return $this->returnApi(200, "查询成功", true, $data);
    }

    /**
     * 单本点击量统计排行榜
     * @param start_time 开始时间
     * @param end_time 结束时间
     * @param big_type_id 系统大类
     * @param resource_business_id  资源商id
     * @param page int 当前页
     * @param limit int 分页大小
     */
    public function resourceAccessNumStatistics()
    {
        $start_time = request()->start_time;
        $end_time = request()->end_time;
        $big_type_id = request()->big_type_id;
        $resource_business_id = request()->resource_business_id;
        $page = $this->request->page ? intval($this->request->page) : 1;
        $limit = $this->request->limit ? intval($this->request->limit) : 10;

        //默认当日
        // if (empty($start_time) || empty($end_time)) {
        //     $start_time = date('Y-m-d 00:00:00');
        //     $end_time = date('Y-m-d H:i:s');
        // }
        $data = $this->model->resourceAccessNumStatistics($resource_business_id, $big_type_id, $start_time, $end_time, $limit);

        if ($data['data']) {
            $data['data'] = $this->addSerialNumber($data['data'], $page, $limit);
            $total_access_num = $data['total_access_num'];
            $data = $this->disPageData($data);
            $data['total_access_num'] = $total_access_num;
            return $this->returnApi(200, "获取成功", true, $data);
        }
        return $this->returnApi(203, "暂无数据");
    }

    /**
     * 类型点击量统计排行榜
     * @param start_time 开始时间
     * @param end_time 结束时间
     * @param big_type_id 系统大类
     * @param resource_business_id  资源商id
     * @param page int 当前页
     * @param limit int 分页大小
     */
    public function resourceTypeStatistics()
    {
        $start_time = request()->start_time;
        $end_time = request()->end_time;
        $big_type_id = request()->big_type_id;
        $resource_business_id = request()->resource_business_id;
        $page = $this->request->page ? intval($this->request->page) : 1;
        $limit = $this->request->limit ? intval($this->request->limit) : 10;

        //默认当日
        // if (empty($start_time) || empty($end_time)) {
        //     $start_time = date('Y-m-d 00:00:00');
        //     $end_time = date('Y-m-d H:i:s');
        // }
        $data = $this->model->resourceTypeStatistics($resource_business_id, $big_type_id, $start_time, $end_time, $limit);

        if ($data['data']) {
            $data['data'] = $this->addSerialNumber($data['data'], $page, $limit);
            $total_access_num = $data['total_access_num'];
            $data = $this->disPageData($data);
            $data['total_access_num'] = $total_access_num;
            return $this->returnApi(200, "获取成功", true, $data);
        }
        return $this->returnApi(203, "暂无数据");
    }


    /**
     * 资源商统计排行榜
     * @param resource_business_id  资源商id
     * @param big_type_id 系统大类
     * @param start_time 开始时间 不传，默认是当天   (时间只对点击量生效)
     * @param end_time 结束时间 不传，默认是当天
     * @param page int 当前页
     * @param limit int 分页大小
     */
    public function resourceBusinessStatistics()
    {
        $resource_business_id = request()->resource_business_id;
        $big_type_id = request()->big_type_id;
        $start_time = request()->start_time;
        $end_time = request()->end_time;
        $page = $this->request->page ? intval($this->request->page) : 1;
        $limit = $this->request->limit ? intval($this->request->limit) : 10;

        //默认当日
        // if (empty($start_time) || empty($end_time)) {
        //     $start_time = date('Y-m-d 00:00:00');
        //     $end_time = date('Y-m-d H:i:s');
        // }
        $data = $this->model->resourceTotalStatistics($resource_business_id, $big_type_id, $start_time, $end_time, $limit);

        if ($data['data']) {
            $data['data'] = $this->addSerialNumber($data['data'], $page, $limit);
            $total_access_num = $data['total_access_num'];
            $total_resource_num = $data['total_resource_num'];
            $data = $this->disPageData($data);
            $data['total_access_num'] = $total_access_num;
            $data['total_resource_num'] = $total_resource_num;
            return $this->returnApi(200, "获取成功", true, $data);
        }
        return $this->returnApi(203, "暂无数据");
    }

    /**
     * 资源类型统计排行榜
     * @param resource_business_id  资源商id
     * @param big_type_id 系统大类
     * @param page int 当前页
     * @param limit int 分页大小
     */
    public function resourceBigTypeStatistics()
    {
        $resource_business_id = request()->resource_business_id;
        $page = $this->request->page ? intval($this->request->page) : 1;
        $limit = $this->request->limit ? intval($this->request->limit) : 10;

        $data = $this->model->resourceBigTypeStatistics($resource_business_id, $page,$limit);

        if ($data['data']) {
            $data['data'] = $this->addSerialNumber($data['data'], $page, $limit);
            $total_access_num = $data['total_access_num'];
            $total_resource_num = $data['total_resource_num'];
            $data = $this->disPageData($data);
            $data['total_access_num'] = $total_access_num;
            $data['total_resource_num'] = $total_resource_num;
            return $this->returnApi(200, "获取成功", true, $data);
        }
        return $this->returnApi(203, "暂无数据");
    }


    /**
     * 用户阅读排行榜
     * @param start_time 开始时间
     * @param end_time 结束时间
     * @param page int 当前页
     * @param limit int 分页大小
     */
    public function userReadStatistics()
    {
        $start_time = request()->start_time;
        $end_time = request()->end_time;
        $page = $this->request->page ? intval($this->request->page) : 1;
        $limit = $this->request->limit ? intval($this->request->limit) : 10;

        //默认当日
        // if (empty($start_time) || empty($end_time)) {
        //     $start_time = date('Y-m-d 00:00:00');
        //     $end_time = date('Y-m-d H:i:s');
        // }
        $resourceUserReadBookDayModel = new ResourceUserReadBookDay();
        $data = $resourceUserReadBookDayModel->userReadStatistics($start_time, $end_time, $limit);

        if ($data['data']) {
            $data['data'] = $this->addSerialNumber($data['data'], $page, $limit);
            $total_read_duration = $data['total_read_duration'];
            $data = $this->disPageData($data);
            $data['total_read_duration'] = $total_read_duration;
            return $this->returnApi(200, "获取成功", true, $data);
        }
        return $this->returnApi(203, "暂无数据");
    }

    /**
     * 用户邀请量统计
     * @param start_time 开始时间
     * @param end_time 结束时间
     * @param keywords string 搜索关键词
     * @param page int 当前页
     * @param limit int 分页大小
     */
    public function userInviteStatistics()
    {
        $start_time = request()->start_time;
        $end_time = request()->end_time;
        $keywords = request()->keywords;
        $page = $this->request->page ? intval($this->request->page) : 1;
        $limit = $this->request->limit ? intval($this->request->limit) : 10;

        $userInfoModel = new UserInfo();
        $data = $userInfoModel->getInviteUserStatistics($start_time, $end_time, $keywords, $limit);

        if ($data['data']) {
            $data['data'] = $this->addSerialNumber($data['data'], $page, $limit);
            $total_number = $data['total_number'];
            $data = $this->disPageData($data);
            $data['total_number'] = $total_number;
            return $this->returnApi(200, "获取成功", true, $data);
        }
        return $this->returnApi(203, "暂无数据");
    }
}
